<template>
  <t-tooltip :content="help" :trigger="cpTooltipTrigger">
    <t-checkbox-group
      v-model="valueThis"
      :options="cpOptions"
      @change="_change"
      v-bind="$attrs"
      :class="{'custom-input-checkbox': true, 'custom-input-checkbox-full-item': fullItem}"
      :readonly="readonly"
      :disabled="disabled"
    />
  </t-tooltip>
</template>

<script>

export default {
  desc: '自定义复选框',
  components: {
  },
  name: 'customCheckbox',
  props: {
    /**
     * @Description：上级页面对象
     * @Date 2023-10-10
     */
    parent: {
      type: Object,
      default: () => {}
    },
    /**
     * @Description：上级表单对象
     * @Date 2023-10-10
     */
    form: {
      type: Object,
      default: () => {}
    },
    /**
     * @Description：输入提示信息
     * @Date 2023-10-10
     */
    help: {
      type: String,
      default: () => ''
    },
    /**
     * @Description：只读
     * @Date 2023-10-10
     */
    readonly: {
      type: Boolean,
      default: () => false
    },
    /**
     * @Description：禁用
     * @Date 2023-10-10
     */
    disabled: {
      type: Boolean,
      default: () => false
    },
    /**
     * @Description：选项独行
     * @Date 2023-10-10
     */
    fullItem: {
      type: Boolean,
      default: () => false
    },
    /**
     * @Description：是否是表单项内部
     * @Date 2023-10-10
     */
    isInnerForm: {
      type: Boolean,
      default: () => true
    },
    /**
     * @Description：非表单项时，v-model 值
     * @Date 2023-10-10
     */
    value: {
      type: Array,
      default: () => []
    }
  },
  created() {
    this.bindFormItemProp()
  },
  data() {
    return {
      valueThis: [],

      // 表单配置
      formItemProp: {
        name: '',
        label: ''
      }
    }
  },
  methods: {
    // 值变化更新表单值
    _change() {
      if (this.isInnerForm) {
        this.parent.$set(this.form, this.formItemProp.name, this.valueThis)
      } else {
        this.parent[this.formItemProp.name] = this.valueThis
      }
      this.$emit('change', this.valueThis)
    },
    /**
     * @Description：获取表单配置
     * @Author wyt
     * @Date 2023-09-27
     */
    bindFormItemProp() {
      if (this.isInnerForm) {
        if (!this.$parent.$options || !this.$parent.$options.propsData) {
          console.error('字段未找到表单项，请检查')
          return false
        }
        this.formItemProp = {
          name: this.$parent.$options.propsData.name,
          label: this.$parent.$options.propsData.label
        }

      } else {
        this.formItemProp = {
          name: this.$attrs.name,
          label: this.$attrs.label
        }
      }
      if (!this.formItemProp.name) {
        return false
      }
      if (!this.formItemProp.label) {
        return false
      }
    },
    /**
     * @Description：聚焦事件
     * @Author wyt
     * @Date 2023-09-27
     */
    focus() {
      this.$emit('focus')
    }
  },
  watch: {
    // 监听表单设置本组件的值
    form: {
      immediate: true, // 初始创建 watch 时立即执行回调函数
      deep: true, // 监听到对象里面的值的变化
      handler() {
        this.valueThis = []
        if (this.form && this.form[this.formItemProp.name]) {
          if (this.form[this.formItemProp.name] && this.form[this.formItemProp.name] instanceof Array) {
            this.valueThis = this.form[this.formItemProp.name]
          } else {
            this.valueThis = [this.form[this.formItemProp.name]]
          }
        }
      },
    },
    // 监听表单设置本组件的值
    value: {
      immediate: true, // 初始创建 watch 时立即执行回调函数
      deep: true, // 监听到对象里面的值的变化
      handler() {
        this.valueThis = []
        if (this.value && this.value.length > 0) {
          this.valueThis = this.value
        }
      },
    }
  },
  computed: {
    cpTooltipTrigger() {
      return this.strings.isBlank(this.help) ? '' : 'hover'
    },

    // 选项
    cpOptions() {
      let options = [{ label: '全选', checkAll: true }]
      if (this.$attrs && this.$attrs.options != undefined && this.$attrs.options.length > 0) {
        options = options.concat(this.$attrs.options)
      }
      return options
    }
  }
}
</script>

<style lang="scss">
  .custom-input-checkbox-full-item{
    .t-checkbox{
      width: 100%;
    }

  }
</style>
